<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/23
 * Time: 11:11
 */

namespace app\api\validate;


use app\lib\exception\BaseException;
use app\lib\exception\ParameterException;
use think\Exception;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{

    public function goCheck(){

        $param = \request()->param();
        $result = $this->batch()->check($param);
        if($result){
            return true;
        }else{
            throw new ParameterException(['msg'=>$this->getError()]);
        }


    }

    // 自定义验证规则
    public function checkID($value,$rule='',$data=[],$field='')
    {
        if(is_numeric($value) && is_int($value+0) && ($value+0)>0){
            return true;
        }
        return false;
    }

    protected function isNotEmpty($value, $rule='', $data='', $field='')
    {
        if (empty($value)) {
            return $field . '不允许为空';
        } else {
            return true;
        }
    }

    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function getDataByRule($data){
        if(array_key_exists('user_id',$data)||array_key_exists('uid',$data)){
            throw new BaseException(['msg'=>'非法传入userid参数']);
        }
        $result = [];
        $rules = $this->rule;
        foreach ($rules as $key=>$val){
            $result[$key]=$data[$key];
        }
        return $result;

    }

}